package com.mmg.spzx.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mmg.spzx.manager.mapper.ProductDetailsMapper;
import com.mmg.spzx.manager.mapper.ProductMapper;
import com.mmg.spzx.manager.mapper.ProductSkuMapper;
import com.mmg.spzx.manager.service.ProductService;
import com.mmg.spzx.model.dto.product.ProductDto;
import com.mmg.spzx.model.entity.product.Product;
import com.mmg.spzx.model.entity.product.ProductDetails;
import com.mmg.spzx.model.entity.product.ProductSku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Override
    public PageInfo<Product> findByPage(Integer page, Integer limit, ProductDto productDto) {
        PageHelper.startPage(page, limit);
        List<Product> productList = productMapper.findByPage(productDto);
        return new PageInfo<>(productList);
    }

    @Override
    public void save(Product product) {
        //保存商品的基本信息到product表中
        product.setStatus(0);
        product.setAuditStatus(0);
        productMapper.save(product);
        //获取商品的sku集合保存到product_sku表中
        product.getProductSkuList().forEach(productSku -> {
            productSku.setSkuCode(product.getId() + "_" + UUID.randomUUID().toString().replaceAll("-", ""));
            productSku.setSkuName(product.getName() + productSku.getSkuSpec());
            productSku.setProductId(product.getId());
            productSku.setSaleNum(0);
            productSku.setStockNum(0);
            product.setStatus(0);
            productSkuMapper.save(productSku);
        });
        //获取商品的详情信息保存到product_details表中
        ProductDetails productDetails = new ProductDetails();
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetails.setProductId(product.getId());
        productDetailsMapper.save(productDetails);
    }

    @Override
    public Product findById(Long id) {
        //根据id查询商品的基本信息
        Product product = productMapper.findById(id);
        //根据id查询商品的sku信息
        List<ProductSku> productSkuList = productSkuMapper.findByProductId(id);
        product.setProductSkuList(productSkuList);
        //根据id查询商品的详情信息
        ProductDetails productDetails = productDetailsMapper.findByProductId(id);
        product.setDetailsImageUrls(productDetails.getImageUrls());
        return product;
    }

    @Override
    public void update(Product product) {
        //更新商品的基本信息
        productMapper.update(product);
        //更新商品的sku信息
        product.getProductSkuList().forEach(productSku -> {
            productSkuMapper.update(productSku);
        });
        //更新商品的详情信息
        ProductDetails productDetails = productDetailsMapper.findByProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.update(productDetails);
    }

    @Override
    public void delete(Long id) {
        //根据商品id删除商品的基本信息
        productMapper.delete(id);
        //根据商品id删除商品的sku信息
        productSkuMapper.deleteByProductId(id);
        //根据商品id删除商品的详情信息
        productDetailsMapper.deleteByProductId(id);
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        if (auditStatus == 1) {
            product.setAuditStatus(1);
            product.setAuditMessage("审批通过");
        } else {
            product.setAuditStatus(-1);
            product.setAuditMessage("审批不通过");
        }
        productMapper.update(product);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if (status == 1) {
            product.setStatus(1);
        } else {
            product.setStatus(-1);
        }
        productMapper.update(product);
    }
}
